package com.news.controller;

import com.news.model.News;
import com.news.model.Record;
import com.news.service.NewsService;
import com.news.service.RecordService;
import com.news.util.UserAcquaintance;
import com.news.util.UserSuanFa;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.function.BiConsumer;

@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private NewsService newsService;

    @RequestMapping("/insert")
    public String insert(String link, HttpSession session, Model model) {
        String id = UUID.randomUUID().toString().replace("-", "");
        User userId = (User) session.getAttribute("user");
        List<Record> record1 = recordService.queryOne(userId.getId());
        List<String> list = new ArrayList<>();
        for (Record re : record1) {
            list.add(re.getNewsid());
        }
        if (!list.contains(link)) {
            Record record = new Record();
            record.setId(id);
            record.setNewsid(link);
            record.setUserid(userId.getId());
            recordService.insert(record);
        }
        model.addAttribute("link", link);
        return "forward:/record/query";
    }

    //每种推荐4条数据不够的用最新的新闻筹够
    @RequestMapping("/query1")
    public String Tuijian(String link, Model model, HttpSession session) {
        List<Record> records = recordService.queryUser();
        UserAcquaintance ua = new UserAcquaintance();
        User user = (User) session.getAttribute("user");
        List<String> useracquaintance = null;
        if (records.size() != 1) {
            useracquaintance = ua.Useracquaintance(records, user.getId());
        }
        //四条 不出现用户已经阅读过的
        List<Record> records1 = recordService.queryOne(user.getId());

        List<News> news = newsService.hostPost();
        news.removeAll(records1);

        List<News> news1 = newsService.orderData();
        List<News> news2 = new ArrayList<>();
        for (int i = 0; i < useracquaintance.size(); i++) {
            News news3 = newsService.queryOne(useracquaintance.get(i));
            news2.add(news3);
        }
        //热点补齐4个
        if (news.size() < 4) {
            for (int i = 0; i < (4 - useracquaintance.size()); i++) {
                news.add(news1.get(4 + i));
            }
        }
        //补齐推荐4个
        if (useracquaintance.size() < 4) {
            for (int i = 0; i < (4 - useracquaintance.size()); i++) {
                news2.add(news1.get(i));
            }
        }
        model.addAttribute("hostPost", news);
        //算法
        model.addAttribute("arithmetic", news2);
        System.out.println(link);
        News news3 = newsService.queryOne(link);
        model.addAttribute("id", news3.getLink());
        return "single";
    }

    @RequestMapping("/query")
    public String Tuijian1(String link, Model model, HttpSession session) {
        List<Record> records = recordService.queryUser();
        Map<String, Integer> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        UserSuanFa us = new UserSuanFa();
        List<News> news1 = newsService.orderData();
        List<News> arithmetic = new ArrayList<>();
        List<News> host = newsService.hostPost();
        Set<String> set = new HashSet<>();
        Set<String> set1 = map.keySet();
        if (records.size() != 1) {
            Map<String, Double> tuijian = us.Tuijian(records, user.getId());
            List<String> sort = us.sort(tuijian);
            for (int i = 0; i < sort.size(); i++) {
                List<Record> records1 = recordService.queryOne(sort.get(i));
                List<Record> records2 = recordService.queryOne(user.getId());
                //用来存储改用户所用访问过的新闻ID
                for (Record re : records2) {
                    set.add(re.getNewsid());
                }
                //获取除此用户没访问过的新闻ID

                for (Record re : records1) {
                    if (!set.contains(re.getNewsid())) {
                        if (set1.contains(re.getNewsid())) {
                            map.put(re.getNewsid(), map.get(re.getNewsid()) + 1);
                        } else {
                            map.put(re.getNewsid(), 0);
                        }
                    }
                }
                }
            map.forEach(new BiConsumer<String, Integer>() {
                @Override
                public void accept(String s, Integer integer) {
                    System.out.println(s+"测试"+integer);
                }
            });
            List<Map.Entry<String, Integer>> list = new ArrayList<>();
            list.addAll(map.entrySet());
            Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue());
            for (int j = 0; j < list.size(); j++) {
                News news = newsService.queryOne(list.get(j).getKey());
                arithmetic.add(news);
            }
            for (int i = 0; i <list.size(); i++) {
                System.out.println(list.get(i));
            }
        }

        model.addAttribute("hostPost", host);
        //算法
        if(arithmetic.size()==0){
            model.addAttribute("arithmetic", news1);
        }else{
            model.addAttribute("arithmetic", arithmetic);
        }
        News news3 = newsService.queryOne(link);
        model.addAttribute("id", news3.getLink());
        return "single";
    }
}
